package com.student.user.service;

import com.student.user.dao.TeacherDao;
import com.student.user.pojo.Teacher;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;
import util.IdWorker;

import java.util.Optional;

@Service
public class TeacherService {

    @Autowired
    private TeacherDao teacherDao;

    @Autowired
    private IdWorker idWorker;

    @Autowired
    private BCryptPasswordEncoder encoder;

    public Teacher login(Teacher teacher) {

        Optional<Teacher> teacherLogin = teacherDao.findById(teacher.getId());

        if(teacherLogin != null && encoder.matches(teacher.getPassword(), teacherLogin.get().getPassword())){
            //保证数据库中的对象中的密码和用户输入的密码是一致的。登录成功
            return teacherLogin.get();
        }
        //登录失败
        return null;

    }

}
